package com.zx.mobile.controller;

import java.util.Map;
import java.util.Objects;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.zx.map.dao.model.StoreInfo;
import com.zx.map.service.StoreInfoService;
import com.zx.map.service.model.MerchantInfoDetailModel;
import com.zx.map.utils.CommonResponseUtils;
import com.zx.mobile.utils.WebUtils;

@Controller
@RequestMapping("/storeInfo")
public class StoreInfoController {

	@Autowired
	private StoreInfoService storeInfoService;
	
	@RequestMapping("/add")
	@ResponseBody
	public Map add(@RequestBody StoreInfo storeInfo, HttpServletRequest request) {
		try {
			Objects.requireNonNull(storeInfo);
			MerchantInfoDetailModel merchant = WebUtils.queryCurrentLoginMerchantInfoDetailModel(request);
			String merchantId = merchant.getMerchantId();
			Objects.requireNonNull(merchantId);
			storeInfo.setMerchantId(merchantId);
			int id = storeInfoService.add(storeInfo);
			if(id == 1) {
				return CommonResponseUtils.successResponse();
			}else {
				return CommonResponseUtils.failureResponse("该商户已认证，请勿重复认证!");
			}
			
		}catch(Throwable e) {
			e.printStackTrace();
			return CommonResponseUtils.failureResponse(e.getMessage());
		}
	}
	@RequestMapping("update")
	@ResponseBody
	public Map update(@RequestBody StoreInfo storeInfo, HttpServletRequest request) {
		try {
			storeInfoService.update(storeInfo);
			return CommonResponseUtils.successResponse();
		}catch(Throwable e) {
			e.printStackTrace();
			return CommonResponseUtils.failureResponse(e.getMessage());
		}
	}
	
	@RequestMapping("find")
	@ResponseBody
	public Map find(@RequestBody Map<String, Object> params, HttpServletRequest request) {
		try {
			MerchantInfoDetailModel merchant = WebUtils.queryCurrentLoginMerchantInfoDetailModel(request);
			String merchantId = merchant.getMerchantId();
			Objects.requireNonNull(merchantId);
			StoreInfo storeInfo = storeInfoService.find(merchantId);
			return CommonResponseUtils.successResponse(storeInfo);
		}catch(Throwable e) {
			e.printStackTrace();
			return CommonResponseUtils.failureResponse(e.getMessage());
		}
	}
}
